﻿using MediatR;
using Purchase.ApiS.Dto;
using Purchase.ApiS.ListDisplay.Command;
using Purchase.Domian.Model;
using Purchase.ErrorCode;
using Purchase.Infrastructrue.Implenment;

namespace Purchase.ApiS.ListDisplay.Hanlder
{
    public class GetQulityCommandHanlder : IRequestHandler<GetQulityCommand, ApiResult<List<QualityDto>>>
    {
        public readonly IBaseRepostity<Quality> _baseRepostity;
        public readonly  IBaseRepostity<InspectionDep> _inspectionDepRepostity;
        public readonly ILogger<GetQulityCommandHanlder> logger;

        public GetQulityCommandHanlder(IBaseRepostity<Quality> baseRepostity, ILogger<GetQulityCommandHanlder> logger, IBaseRepostity<InspectionDep> inspectionDepRepostity)
        {
            _baseRepostity = baseRepostity;
            this.logger = logger;
            _inspectionDepRepostity = inspectionDepRepostity;
        }
        /// <summary>
        /// 质检信息的列表展示方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<QualityDto>>> Handle(GetQulityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<QualityDto>> result = new ApiResult<List<QualityDto>>();
            try
            {
                var mete = _baseRepostity.GetAll();
                var DEP = _inspectionDepRepostity.GetAll();
                var list = from m in mete
                           join d in DEP on m.QuaDepId equals d.InsDepId
                           select new QualityDto()
                           {
                               QuaId = m.QuaId,
                               QuaName = m.QuaName,
                               QuaCode = m.QuaCode,
                               QualityInspector = m.QualityInspector,
                               QuaState = m.QuaState,
                               QuaDate = m.QuaDate,
                               InsDepName = d.InsDepName,
                               LDate = m.LDate,
                               


                           };
                result.Data = list.ToList();
                result.Msg = "获取质检信息成功";
                logger.LogInformation("获取质检信息成功");
                result.Code = ApiEume.Success;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取质检信息失败");
               
            }
            return Task.FromResult(result);
        }
    }
}
